﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using LilTmSimFacade;
using StateMachine;
using Microsoft.Win32;
using System.ComponentModel;

namespace LilTmSim
{
	/// <summary>
	/// Interaction logic for MainWindow.xaml
	/// </summary>
	public partial class MainWindow : Window
	{
		public IMachineTab CurrentTab
		{
			get { return TabControlsView.CurrentItem as IMachineTab; }
		}

		public IMachineTabCollection TabControls
		{
			get { return Resources["tabCollection"] as IMachineTabCollection; }
		}

		public ICollectionView TabControlsView
		{
			get { return CollectionViewSource.GetDefaultView(TabControls); }
		}

		public MainWindow()
		{
			InitializeComponent();
		}

		private void NewTab()
		{
			NewTab(new TuringTab());
		}

		private void NewTab(string path)
		{
			NewTab(new TuringTab(path));
		}

		private void NewTab(IMachineTab tab)
		{
			TabControls.Add(tab);
			TabControlsView.MoveCurrentTo(tab);
		}

		private void Save(IMachineTab tab)
		{
			if (tab.HasBeenSaved)
				tab.Save();
			else
				SaveAs(tab);
		}

		private void SaveAs(IMachineTab tab)
		{
			SaveFileDialog save = new SaveFileDialog();
			save.Filter = "Neumont University Machines(*.num)|*.num|JFlap Files(*.jff)|*.jff|All Files|*.*";
			save.InitialDirectory = System.Environment.GetFolderPath(System.Environment.SpecialFolder.MyDocuments);
			if (save.ShowDialog(this).Value)
			{
				tab.Save(save.FileName);
			}
		}

		private void AlwaysCanExecute(object sender, CanExecuteRoutedEventArgs e) { e.CanExecute = true; }
		private void MenuNew_Click(object sender, ExecutedRoutedEventArgs e) { NewTab(); }
		private void MenuSave_Click(object sender, ExecutedRoutedEventArgs e) { Save(CurrentTab); }
		private void MenuSaveAs_Click(object sender, ExecutedRoutedEventArgs e) { SaveAs(CurrentTab); }
		private void MenuClose_Click(object sender, ExecutedRoutedEventArgs e) { Close(); }

		private void MenuOpen_Click(object sender, ExecutedRoutedEventArgs e) 
		{
			OpenFileDialog open = new OpenFileDialog();
			open.Filter = "Machines(*.num, *.jff)|*.num;*.jff|Neumont University Machines(*.num)|*.num|JFlap Files(*.jff)|*.jff|All Files|*.*";
			open.Multiselect = true;
			open.InitialDirectory = System.Environment.GetFolderPath(System.Environment.SpecialFolder.MyDocuments);
			if (open.ShowDialog(this).Value)
			{
				foreach (string path in open.FileNames)
				{
					NewTab(path);
				}
			}
		}

		private void TabClose_Click(object sender, RoutedEventArgs e)
		{
			Button button = sender as Button;
			IMachineTab tab = button != null ? button.DataContext as IMachineTab : null;
			if (tab != null)
			{
				MessageBoxResult result = MessageBox.Show("Would you like to save changes to this file?", "Save Changes", MessageBoxButton.YesNoCancel, MessageBoxImage.Question, MessageBoxResult.Cancel);
				if (result == MessageBoxResult.Yes)
					Save(tab);
				if (result != MessageBoxResult.Cancel)
				{
					if (!TabControlsView.MoveCurrentToNext())
						TabControlsView.MoveCurrentToPrevious();
					TabControls.Remove(tab);
					if (TabControls.Count == 0)
					{
						TabControls.Add(new TuringTab());
						TabControlsView.MoveCurrentToFirst();
					}
				}
			}
		}
	}
}
